#include "settingmanage.h"
#include <QCoreApplication>
#include <QDir>

SettingManage::SettingManage()
{
    settings = new QSettings(QCoreApplication::applicationDirPath() + "/NotesQT.cfg", QSettings::IniFormat);
}

QSettings * SettingManage::getSettings()
{
    return settings;
}

QString SettingManage::getRootPath()
{
    QString rootPath = settings->value("Notes/RootPath", "NOTES_DATA").toString();

    rootPath = rootPath.replace("\\", "/");

    bool b1 = (rootPath.left(1) == "/");
    bool b2 = rootPath.contains(':');
    if (b1 || b2) {
        return rootPath;
    }
    QString path = QCoreApplication::applicationDirPath();
    QDir dir(path);
    dir.cdUp();
    path = dir.path();
    path.append("/").append(rootPath);
    return path;
}

QFont SettingManage::getListFont()
{
    QString listFontFamily = settings->value("List/fontFamily", "Microsoft YaHei UI").toString();
    bool listFontBold = settings->value("List/fontBold", false).toBool();
    int listFontSize = settings->value("List/fontSize", 12).toInt();

    QFont listFont;
    listFont.setFamily(listFontFamily);
    listFont.setBold(listFontBold);
    listFont.setPointSize(listFontSize);

    return listFont;
}

QFont SettingManage::getTextFont()
{
    QString fontFamily = settings->value("TextEdit/fontFamily", "Courier").toString();
    int fontSize = settings->value("TextEdit/fontSize", "19").toInt();
    bool fontBold = settings->value("TextEdit/fontBold", "false").toBool();

    QFont font;
    font.setFamily(fontFamily);
    font.setPointSize(fontSize);
    font.setBold(fontBold);
    return font;
}

void SettingManage::setTextFont(QFont font)
{
    settings->setValue("TextEdit/fontFamily", font.family());
    settings->setValue("TextEdit/fontSize", font.pointSize());
    settings->setValue("TextEdit/fontBold", font.bold());
}
